/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.semi.fw;

import com.semi.lex.Token;
import com.semi.util.FormatException;
import com.semi.util.IPv4Util;
import com.semi.util.StringUtil;

/**
 *
 * @author liaoyq
 */
public class UnCoderIP extends UnRule {
    public int  mIP=0;
    public int mMask=0;
    private int mHost=0;
    public long mTTL=0;
    public UnCoderIP(){
        
    }
    /**
     * 
     * @param ip  IP地址，例如192.168.1.1
     * @param mask  子网掩码，例如255.255.255.0
     * @throws com.semi.util.FormatException
     */
    public UnCoderIP(String ip,String mask) throws FormatException{
        mIP = IPv4Util.ipToInt(ip);
        if( mask.length()<2 ) mMask = IPv4Util.MASK_SINGLE;
        else mMask = IPv4Util.ipToInt(mask);
        mHost = mIP & mMask;
    }   
    /**
     * 
     * @param ipseg  网络段表示，例如192.168.1.1/24
     * @throws com.semi.util.FormatException
     */
    public UnCoderIP(String ipseg) throws FormatException{
        int[] a = IPv4Util.getIPIntScope(ipseg);
        mIP = a[0];
        mHost = a[1];
        mMask = a[2];
    }
    public int setProp(String prop,String value) throws FormatException {
        if( value == null ) return 0;
        if( value.trim().length()<1 ) return 0;
        if( "host".compareToIgnoreCase(prop)==0 ) {
            if( !setIP(value,null) ) return 0;
        }
        else if( "ttl".compareToIgnoreCase(prop)==0 ){
            Long l  = StringUtil.convertToLong(value);
            if( l== null ) return 0;
            if( l==0 ) mTTL=0; else mTTL = System.currentTimeMillis()+l;
        }
        else if( "enable".compareToIgnoreCase(prop)==0 ) {
            try{
               mEnable = StringUtil.convertToBool(value);
            }catch(Exception e){
                return 0;
            }
        } else return -1;
        return 1;
    }
    public String dump(){
        return "host["+IPv4Util.intToIp(mIP)+"/"+IPv4Util.maskToInt(mMask)+"] ttl["+mTTL+"] enable["+mEnable+"]";
    }
    public String toCmd(){
        int k;
        if( mTTL==0 ) k=0; else k = (int)(mTTL-System.currentTimeMillis())/1000;
        if( k<0 ) return "";
        String tp =  "add ip host "+IPv4Util.intToIp(mIP);
        int x = IPv4Util.maskToInt(mMask);
        if( x<32 ) tp += "/"+x;
        if( k!=0 ) tp += " ttl "+  k ;
        if( !mEnable) tp +=" enable false";
        return tp;
    }
    public String getName(){
        return IPv4Util.intToIp(mIP)+"-"+IPv4Util.intToIp(mMask);
    }
    public boolean setIP(String ip,String mask) throws FormatException{
        if( ip.length()<4 ) throw new FormatException("invalid ip");
        if( ip.contains("/")){
            int[] a = IPv4Util.getIPIntScope(ip);
            mIP = a[0];
            mHost = a[1];
            mMask = a[2];                
        } else {
            mIP = IPv4Util.ipToInt(ip);
            if( mask==null || mask.length()<4 ) mMask = IPv4Util.MASK_SINGLE;
            else mMask = IPv4Util.ipToInt(mask);
            mHost = mIP & mMask;
        }
        return true;
    }
    public void enable(long next){
        if( next>0 ){
            mTTL = System.currentTimeMillis()+next;
        } else mTTL = 0;
        mEnable = true;
    }
    @Override
    public boolean validate(){
        if( mEnable ){
            if( mTTL==0 ) return true;
            if( mTTL>System.currentTimeMillis() ) return true;
        }
        return false;
    }
    public boolean match(int in){
        if( validate() ){
            int s = in & mMask;
            return (mHost== s);
        }
        return false;
    }
    public boolean match(String ip){
        if( !validate() ) return false;
        try{
            int s = IPv4Util.ipToInt(ip) & mMask;
            if( mHost== s ) return true;
        }catch(FormatException e){}
        return false;
    }
}
